/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import net.cellobject.javacodeformatter.log.FormatterLog;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

public class DeleteResourceHandler extends AbstractHandler {
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		final ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (! (selection instanceof IStructuredSelection)) {
			return null;
		}
		final IStructuredSelection structSel = (IStructuredSelection)selection;
		// Build a collection of selected resources
		final Collection<IResource> resources = new HashSet<IResource>();
		for (final Iterator<?> iter = structSel.iterator(); iter.hasNext();) {
			Object element = iter.next();
			if (element instanceof IAdaptable) {
				element = ((IAdaptable)element).getAdapter(IResource.class);
			}
			if ( (element instanceof IResource)) {
				resources.add((IResource)element);
			}
		}
		// Execute the operation
		try {
			// Display progress either using the ProgressMonitorDialog ...
			// Shell shell = HandlerUtil.getActiveShell(event);
			// IRunnableContext context = new ProgressMonitorDialog(shell);
			// ... or using the window's status bar
			// IWorkbenchWindow context = HandlerUtil.getActiveWorkbenchWindow(event);
			// ... or using the workbench progress service
			final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
			final IRunnableContext context = window.getWorkbench().getProgressService();
			context.run(true, false, new IRunnableWithProgress() {
				public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					new DeleteResourceOperation(resources.toArray(new IResource[resources.size()])).run(monitor);
				}
			});
		} catch (final Exception e) {
			FormatterLog.logError(e);
		}
		return null;
	}
}
